<?php
/**
 * Public listing of blogposts, with various list filters
 *
 * @version 0.1 
 * @license MIT License http://www.opensource.org/licenses/mit-license.php
 * @copyright (c) 2007 Geir Arne Brevik
 * @author Geir Arne Brevik
 */
include_once('lib.php'); // include bootstrapping and helper functions

// get blogposts
if (isset($_GET['show']) and $_GET['show'] == 'all'){ // all posts, no matter what
	$doc_title = 'All posts';
	$blogposts = MiniBlog::index();
} else 	if (isset($_GET['u']) and MiniBlog::authorExists($_GET['u'])){ // all posts by given user
	$user_id   = $_GET['u'];
	$doc_title = 'Blogposts by '.$user_id;
	$blogposts = MiniBlog::index(array('show_user'=>$user_id));
} else 	if (isset($_GET['u']) and !MiniBlog::authorExists($_GET['u'])){ // if wrong user id
	header("HTTP/1.1 404 Not Found");
	$doc_title = 'Author not found';
	$blogposts = false;
} else { // all posts marked for the frontpage
	$doc_title = 'Frontpage';
	$blogposts = MiniBlog::index(array('visible_on_frontpage'=>true));
}

include('meta.php');
include('header.php');

if ($blogposts){
	?><div id="blogposts"><?php // wrapping div
	foreach($blogposts as $blogpost) { // loop through the blogpost objects
		?><div class="blogpost">
		<h3><a href="<?php echo($blogpost->permalink); ?>" title="<?php printf('Go to %s', $blogpost->title); ?>"><?php echo($blogpost->title); ?></a><?php echo(($_GET and $blogpost->visible_on_frontpage and $blogpost->frontpage_allowed) ? "  ★" : ""); ?></h3><?php
		echo(filter(cutTooLong($blogpost->body, array('continue_uri'=>$blogpost->permalink, 'max_length'=>100))));
		?>
		<p class="credits">By <?php echo(userLink($blogpost->created_by).', '.showDate($blogpost->created_at)); ?></p>
		</div><?php
	}
	?></div><?php
} else {
	echo('No blogposts found');
}
?>


<?php
include('footer.php');
?>